﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Input;
using UtilityLibrary;

namespace MapLibrary
{
    public class CollisionRectangle : ITransformable
    {
        private Rectangle rect;
        private Rectangle transRect;
        private Vector2 scale;
        float rotation;
        bool isActive;

        public CollisionRectangle(Rectangle rect)
        {
            this.rect = rect;
            this.transRect = new Rectangle();
            scale = new Vector2(1.0f, 1.0f);
            rotation = 0.0f;
            isActive = true;
        }

        public void Reset()
        {
            rect = new Rectangle();
            transRect = new Rectangle();
            scale = new Vector2(1.0f, 1.0f);
            rotation = 0.0f;
            isActive = true;
        }

        public void TransformToWorldCoords(WorldViewport viewport)
        {
            Vector2 pos = new Vector2(rect.X, rect.Y);
            pos = viewport.ViewportCoordsToWorldCoords(pos);
            rect.X = (int)pos.X;
            rect.Y = (int)pos.Y;
        }

        public CollisionRectangle Clone()
        {
            transRect.X = this.rect.X;
            transRect.Y = this.rect.Y;
            transRect.Width = (int)(this.rect.Width * scale.X);
            transRect.Height = (int)(this.rect.Height * scale.Y);
            CollisionRectangle rect = new CollisionRectangle(transRect);

            return rect;
        }

        public Rectangle Rect
        {
            set { rect = value; }
            get { return rect; }
        }

        public float Rotation
        {
            get { return rotation; }
        }

        public void Draw(SpriteBatch batch, WorldViewport viewport)
        {
            transRect.Width = (int)(rect.Width * scale.X);
            transRect.Height = (int)(rect.Height * scale.Y);
            transRect.X = rect.X;
            transRect.Y = rect.Y;
            Vector2 pos = new Vector2();
            pos.X = transRect.X;
            pos.Y = transRect.Y;
            pos = viewport.WorldToViewportCoords(pos);
            transRect.X = (int)pos.X;
            transRect.Y = (int)pos.Y;
            PrimitiveBrush.RenderRect(batch, transRect);
        }

        public bool CheckMouseCollision(GameInput input, WorldViewport viewport)
        {
            transRect.Width = (int)(rect.Width * scale.X);
            transRect.Height = (int)(rect.Height * scale.Y);
            transRect.X = rect.X;
            transRect.Y = rect.Y;
            Vector2 pos = new Vector2();
            pos.X = transRect.X;
            pos.Y = transRect.Y;
            pos = viewport.WorldToViewportCoords(pos);
            transRect.X = (int)pos.X;
            transRect.Y = (int)pos.Y;
            return input.IsMouseInRectangle(transRect, 0.0f);
        }

        public void SetPosition(Vector2 Position)
        {
            rect.X = (int)Position.X;
            rect.Y = (int)Position.Y;
        }

        public Vector2 GetPosition()
        {
            return new Vector2(Rect.X, Rect.Y);
        }

        public void SetRotation(float rotation)
        {
            //not supported
        }

        public Vector2 GetCenter()
        {
            transRect.Width = (int)(rect.Width * scale.X);
            transRect.Height = (int)(rect.Height * scale.Y);
            transRect.X = rect.X;
            transRect.Y = rect.Y;

            return new Vector2(transRect.Width/2, transRect.Height/2);
        }

        public float GetRotation()
        {
            return 0.0f;
        }

        public void SetScale(Vector2 Scale)
        {
            scale = Scale;
        }

        public Vector2 GetScale()
        {
            return scale;
        }

        public bool IsActive()
        {
            return isActive;
        }
    }
}
